package com.wanmao.test.service;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.wanmao.test.bean.ProjectBean;
import com.wanmao.test.bean.RYRWBean;
import com.wanmao.test.dbhelper.DBOpenHelper;

import java.util.ArrayList;
import java.util.List;


public class ProjectService {
    private DBOpenHelper dBOpenHelper;

    public ProjectService(Context context) {
        this.dBOpenHelper = new DBOpenHelper(context);
    }
    private ContentValues settingValue(ProjectBean bean) {
        ContentValues values = new ContentValues();
        values.put("RWID",bean.getRWID());
        values.put("RWMC",bean.getRWMC());
        values.put("JDJD",bean.getJDJD());
        values.put("ZYLX",bean.getZYLX());
        values.put("HDSJ",bean.getHDSJ());
        values.put("HDNR",bean.getHDNR());
        values.put("DZHXL",bean.getDZHXL());
        values.put("DZHXLID",bean.getDZHXLID());

        return values;
    }
    public void save(ProjectBean bean) {
        SQLiteDatabase db = dBOpenHelper.getWritableDatabase();
        db.insert("TestList", null, settingValue(bean));
    }
    public ProjectBean update(String rwid, String HDNR, String DZHXL, String DZHXLID) {
        SQLiteDatabase db = dBOpenHelper.getWritableDatabase();
        String sql="update TestList set HDNR='"+HDNR+"',DZHXL='"+DZHXL+"',DZHXLID='"+DZHXLID+"' where RWID='"+rwid+"'";
        Cursor cursor = db.rawQuery(sql, null);
        if (cursor.moveToNext()){
            return toBean(cursor);
        }

        return null;
    }

    public List<ProjectBean> findAllByList(List<RYRWBean> list){
        SQLiteDatabase db = dBOpenHelper.getReadableDatabase();
        List<ProjectBean> listRW=new ArrayList<>();
        for(int i =0;i<list.size();i++){
            String RWID = list.get(i).getRWID();
            String sql = "select * from TestList where RWID=?";
            Cursor cursor = db.rawQuery(sql, new String[]{RWID});
            ProjectBean bean=toBean(cursor);
            listRW.add(bean);
        }
        return listRW;
    }


    public ProjectBean findByRWID(String rwid) {
        String sql = "select * from TestList where RWID=?";

        SQLiteDatabase db = dBOpenHelper.getWritableDatabase();
        Cursor cursor = db.rawQuery(sql,new String[]{rwid});

        while (cursor.moveToNext()){
            return toBean(cursor);
        }
        cursor.close();
        db.close();
        return null;
    }

    private ProjectBean toBean(Cursor cursor) {
        ProjectBean bean = new ProjectBean();
        if (cursor.moveToFirst()) {
            bean.setRWID(cursor.getString(cursor.getColumnIndex("RWID")));
            bean.setRWMC(cursor.getString(cursor.getColumnIndex("RWMC")));
            bean.setJDJD(cursor.getString(cursor.getColumnIndex("JDJD")));
            bean.setZYLX(cursor.getString(cursor.getColumnIndex("ZYLX")));
            bean.setHDNR(cursor.getString(cursor.getColumnIndex("HDNR")));
            bean.setDZHXL(cursor.getString(cursor.getColumnIndex("DZHXL")));
            bean.setDZHXLID(cursor.getString(cursor.getColumnIndex("DZHXLID")));
            bean.setHDSJ(cursor.getString(cursor.getColumnIndex("HDSJ")));
        }
        return bean;
    }

}
